package br.com.tiespec.ejb.Ejb;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;

import br.com.tiespec.ejb.Dao.AtorDAO;
import br.com.tiespec.ejb.Dao.ClientePortalDAO;
import br.com.tiespec.ejb.EjbInterface.AtorInterface;
import br.com.tiespec.model.Enum.TipoSortOrder;
import br.com.tiespec.model.Modelo.Ator;
import br.com.tiespec.model.Util.DAOException;
import br.com.tiespec.model.Util.RNException;

@Stateless(mappedName = "AtorImpl")
@Local(value=AtorInterface.class)
public class AtorImpl implements AtorInterface {

	private static final long serialVersionUID = 1L;
	
	@EJB AtorDAO aDao;
	@EJB ClientePortalDAO cpDao;
	
	@Override
	public List<Ator> listarAtor(int first, int pageSize, String sortField,
			TipoSortOrder sortOrder, Long idClientePortal, String nome) {
		return aDao.listarAtor(first, pageSize, sortField, sortOrder, idClientePortal, nome);
	}

	@Override
	public Integer countAtor(Long idClientePortal, String nome) {
		return aDao.countAtor(idClientePortal, nome);
	}

	@Override
	public Ator selecionarAtor(Long id) {
		return aDao.selecionar(id);
	}
	
	@Override
	public void removerAtor(Long id) throws RNException {
		try {
			aDao.remover(id);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}

	@Override
	public void atualizarAtor(Ator atorInstance) throws RNException {
		try {
			aDao.update(atorInstance);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}

	@Override
	public void salvarAtor(Ator atorInstance) throws RNException {
		try {
			aDao.save(atorInstance);
		} catch (DAOException e) {
			throw new RNException(e);
		}
	}
	
	@Override
	public Boolean validateNomeUnico(String nome, Long idClientePortal, Long idAtor) {
		Integer count = aDao.buscarAtorPorNomeECliente(nome, idClientePortal, idAtor);
		if (count == 0)
			return true;
		return false;
	}

}
